/*
function initialize() {
 geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(33.578015, 37.96875);
    var myOptions = {
      zoom: 2,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
   map = new google.maps.Map($("#map_canvas")[0],
        myOptions);
		marker = new google.maps.Marker({draggable: true});
		var radiusOptions = {
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 1,
            fillColor: '#FF0000',
            fillOpacity: 0.35,
            map: map,
            center: latlng, //results[0].geometry.location,
            radius: 500//parseFloat($('.when_popup '+'#radius').val())
          };
          circle = new google.maps.Circle(radiusOptions);
		  
		 
  }
*/

var map;
var marker;
var geocoder;
var circle;
var iph_msg_type='base';
var camel_phase_curent=0;
var login_sucess=0;
var push_sended_ok=0;
var choise_app;
var location_type=0;


/*Init map function*/
function initialize() {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(33.578015, 37.96875);
	var myOptions = {
		zoom : 2,
		center : latlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map($("#map_canvas")[0], myOptions);
	marker = new google.maps.Marker({
		draggable : true
	});
	var radiusOptions = {
		strokeColor : '#FF0000',
		strokeOpacity : 0.8,
		strokeWeight : 1,
		fillColor : '#FF0000',
		fillOpacity : 0.35,
		map : map,
		center : latlng, //results[0].geometry.location,
		radius : 500//parseFloat($('.when_popup '+'#radius').val())
	};
	
	
	
	circle = new google.maps.Circle(radiusOptions);
	

	// Create the DIV to hold the control and call the HomeControl() constructor
	// passing in this DIV.
	var homeControlDiv = document.createElement('div');
	var homeControl = new MapControl(homeControlDiv, map, 'Click to set the map to Home', '<strong>Home</strong>', function() {
		getLocation();
	});
	

	homeControlDiv.index = 1;
	map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
	var FullScrControlDiv = document.createElement('div');
	var homeControl = new MapControl(FullScrControlDiv, map, 'Click to view in fulldcreen', '<strong>FullScreen</strong>', function() {
		if ($('#map_canvas').css('position') == 'absolute') {
			$('#map_canvas').css({
				'position' : 'fixed',
				'top' : '5%',
				'left' : '5%',
				'width' : '90%',
				'height' : '90%',
				'z-index' : '1000'
			});
			google.maps.event.trigger(map, 'resize');
			map.setCenter(marker.getPosition());
		} else {
			
			$('#map_canvas').css({
				'position' : 'absolute',
				'top' : '5px',
				'left' : '136px',
				'width' : '588px',
				'height' : '247px',
			});
			google.maps.event.trigger(map, 'resize');
			map.setCenter(marker.getPosition());
		}
	});

	FullScrControlDiv.index = 1;
	map.controls[google.maps.ControlPosition.TOP_RIGHT].push(FullScrControlDiv);

	google.maps.event.addListener(marker, 'dragend', function() {
		location_change('marker');
	});

	var layer = new google.maps.FusionTablesLayer({

		query : {
			select : 'geometry',
			from : '1DXQ1abPVimO3hB_gLqBlOmgiSiTF7I1Zz-kRR4k'
		},
		suppressInfoWindows : false,
	});
	layer.setMap(map);

	google.maps.event.addListener(layer, 'click', function(e) {

		console.log(e.row.continent.value);
	});
}
/* end Init map function*/
 
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  //else{$('#location').html("Geolocation is not supported by this browser."); }
  } 
  
 
function MapControl(controlDiv, map, title, html, func) {

	// Set CSS styles for the DIV containing the control
	// Setting padding to 5 px will offset the control
	// from the edge of the map.
	controlDiv.style.padding = '5px';

	// Set CSS for the control border.
	var controlUI = document.createElement('div');
	controlUI.style.backgroundColor = 'white';
	controlUI.style.borderStyle = 'solid';
	controlUI.style.borderWidth = '2px';
	controlUI.style.cursor = 'pointer';
	controlUI.style.textAlign = 'center';
	controlUI.title = title;
	controlDiv.appendChild(controlUI);

	// Set CSS for the control interior.
	var controlText = document.createElement('div');
	controlText.style.fontFamily = 'Arial,sans-serif';
	controlText.style.fontSize = '12px';
	controlText.style.paddingLeft = '4px';
	controlText.style.paddingRight = '4px';
	controlText.innerHTML = html;
	controlUI.appendChild(controlText);

	// Setup the click event listeners: simply set the map to Chicago.
	google.maps.event.addDomListener(controlUI, 'click', func);
}


function showPosition(position)
  {
  marker.setMap();
	$('.when_popup '+'#lat').val(position.coords.latitude);
	$('.when_popup '+'#lng').val(position.coords.longitude);
	var myLatlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
	map.setCenter(myLatlng);
	marker.setOptions({
	position: myLatlng,
	
      	});
            marker.setMap(map); 
			marker.setAnimation(google.maps.Animation.DROP);
			circle.setMap();
			circle.setOptions({
			center: myLatlng,
            radius: parseFloat($('.when_popup '+'#radius').val())
			});
			circle.setMap(map);
    //$('#long').val(position.coords.longitude);
    //$('#lat').val(position.coords.latitude);
    //$('#location').html("There You are =)");
  //$('#location').val("{\"Latitude\" : \""+ position.coords.latitude + "\" , \"Longitude\" : \"" + position.coords.longitude+"\"}");
  console.log("{\"Latitude\" : \""+ position.coords.latitude + "\" , \"Longitude\" : \"" + position.coords.longitude+"\"}");
  //$('#order-form').submit();  
  }
  

function location_type_change() {
	$('#One_point, #City, #Country, #Continent').hide();
	//console.log($('#location_menu').val());
	switch ($('#location_menu').val()) {
		case 'One_point':
			$('#One_point').show();
			break;
		case 'City':
			$('#City').show();
			break;
		case 'Country':
			$('#Country').show();
			break;
		case 'Continent':
			$('#Continent').show();
			break;
	}
}


function location_change(type) {
	console.log('address' + address + 'radius ' + $('.when_popup ' + '#radius').val());
	switch (type) {
		case 'address':
			{
				var address = $('.when_popup ' + '#address').val();
				alert("address");
				console.log('address' + address + 'radius ' + $('.when_popup ' + '#radius').val());
				geocoder.geocode({
					'address' : address
				}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						marker.setMap();
						console.log(results[0].geometry.location.lat() + '/' + results[0].geometry.location.lng());
						$('.when_popup ' + '#lat').val(results[0].geometry.location.lat());
						$('.when_popup ' + '#lng').val(results[0].geometry.location.lng());
						map.setCenter(results[0].geometry.location);
						marker.setOptions({
							position : results[0].geometry.location
						});
						marker.setMap(map);
						marker.setAnimation(google.maps.Animation.DROP);
						circle.setMap();
						circle.setOptions({
							center : results[0].geometry.location,
							radius : parseFloat($('.when_popup ' + '#radius').val())
						});
						circle.setMap(map);
					} else {
						alert('Geocode was not successful for the following reason: ' + status);
					}
				});
			}
			break
		case 'radius':
			{
				circle.setMap();
				circle.setOptions({
					//center: results[0].geometry.location,
					radius : parseFloat($('.when_popup ' + '#radius').val())
				});
				circle.setMap(map);
			}
			break
		case 'marker': {
			map.setCenter(marker.getPosition());

			$('.when_popup ' + '#lat').val(marker.getPosition().lat());
			$('.when_popup ' + '#lng').val(marker.getPosition().lng());
			geocoder.geocode({
				'location' : marker.getPosition()
			}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					console.log(results[0].formatted_address);
					$('.when_popup ' + '#address').val(results[0].formatted_address);
				}
			});
			circle.setMap();
			circle.setOptions({
				center : marker.getPosition(),
				radius : parseFloat($('.when_popup ' + '#radius').val())
			});
			circle.setMap(map);
		}
		case 'Continent':
			console.log($('.when_popup #address_Continent').val());
			break;
	}
}
